#include "double_list.h"
#include "element.h"

Double_link_List *Init_Double_Link_List(void)
{
    Double_link_List *list = (Double_link_List *)malloc(sizeof(Double_link_List));
    if (NULL == list)
    {
        printf("创建失败\n");
        return NULL;
    }
    list->head = NULL;
    return list;
}

void insert_double_node(Double_link_List *list, Stu_t data, int index)
{
    if (NULL == list)
    {
        printf("表不存在\n");
        return;
    }
    Double_Node * new_node = (Double_Node *)malloc(sizeof(Double_Node));
    if (NULL == new_node)
    {
        printf("创建失败\n");
        return;
    }
    new_node->data = data;
    new_node->next = NULL;
    new_node->prev = NULL;
    if (index == 0 || list->head == NULL)
    {
        new_node->next = list->head;
        if (list->head != NULL)
        {
            list->head->prev = new_node;
        }
        list->head = new_node;
        return;
    }
    
    Double_Node * p = list->head;
    for (int i = 0; i < index -1 && p->next != NULL; i++)
    {
        p = p->next;
    }
    if (NULL == p)
    {
        printf("为空\n");
        return;
    }
    new_node->next = p->next;
    if (NULL != p->next)
    {
        p->next->prev = new_node;
    }
    new_node->prev = p;
    p->next = new_node;
    
}

void print_forward_double_link_list(Double_link_List * list)
{
    Double_Node *L = list->head;
     while (NULL != L)
    {
        printf("姓名 :%s\t",L->data.name);
        printf("性别 :%s\t",L->data.sex);
        printf("年龄 :%d\t",L->data.age);
        printf("学号 :%d\t",L->data.id);
        printf("成绩 :%d\n",L->data.sco);
        L = L->next;
    }
    return;
}

void print_backwards_double_link_list(Double_link_List * list)
{
     Double_Node *L = list->head;
     for (int i = 0; L->next != NULL; i++)
     {
        L = L->next;
     }
     while (NULL != L)
    {
        printf("姓名 :%s\t",L->data.name);
        printf("性别 :%s\t",L->data.sex);
        printf("年龄 :%d\t",L->data.age);
        printf("学号 :%d\t",L->data.id);
        printf("成绩 :%d\n",L->data.sco);
        L = L->prev;
    }
    return;
}

void delet_double_node(Double_link_List * list,int index)
{
    if (NULL == list)
    {
        printf("表不存在\n");
        return;
    }
    if (list->head == NULL)
    {
        printf("节点为空");
    }
    
    Double_Node * p = list->head;
    Double_Node * q = NULL;
    if (index == 0)
    {
        p->next->prev = NULL;
        list->head = p->next;
        free(p);
        return;
    }
    for (int i = 0; i < index - 1 && p->next != NULL; i++)
    {
        p = p->next;
    }
    q = p->next;
    if (q->next == NULL)
    {
        p->next = q->next;
        free(q);
        return ;
    }
    else
    {
        p->next = q->next;
        q->next->prev = p;
        free(q);
    }
    return;
}

Double_Node* Find_Node(Double_link_List *list, int id)
{
    Double_Node * L = NULL;
    L = list->head;
    L->next->prev = L;
    if (list == NULL)
    {
        printf("表不存在");
        return NULL;
    }
    while (L != NULL)
    {
        if (L->data.id == id)
        {
            break;
        }
        L = L->next;
    }
    if (L == NULL)
    {
        printf("查无此人");
    }
     while (NULL != L)
    {
        printf("姓名 :%s\t",L->data.name);
        printf("性别 :%s\t",L->data.sex);
        printf("年龄 :%d\t",L->data.age);
        printf("学号 :%d\t",L->data.id);
        printf("成绩 :%d\n",L->data.sco);
        break;
    }
    
    return L;
}

void data_double_node(Double_link_List *list,int id)
{
    Double_Node *S = Find_Node(list,id);
    int num = 0;
    printf("请输入成绩\n");
    scanf("%d",&num);
    S->data.sco = num;
}

Double_link_List* merge_double_link_lists(Double_link_List* list1, Double_link_List* list2)
{
    if (list1 == NULL || list2 == NULL)
    {
        printf("表为空\n");
        return NULL;
    }
    Double_link_List * list3 = Init_Double_Link_List();
    Double_Node * l = list1->head;
    int index = 0;
    while (l != 0)
    {
        insert_double_node(list3,l->data,index);
        index++;
        l = l->next;
    }
    l = list2->head;
    index = 0;
    while (l != 0)
    {
        insert_double_node(list3,l->data,index);
        index++;
        l = l->next;
    }
    return list3;
}

void free_double_link_list(Double_link_List* list)
{
        if (NULL == list)
    {
        printf("表不存在\n");
    }
    Double_Node * p = list->head;
    Double_Node * q = NULL;

    while (p != NULL)
    {
        q = p;
        p = p->next;
        free(q);
        q = NULL;
    }
    free(list);
    return;
}

